<template>
    <div class="comment-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要评论的内容" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment()">发表评论</mt-button>
        <ul class="cmt-list">
            <li class="cmt-item" v-for="(item,index) in comments" :key="item.add_time">
                <div class="cmt-title">
                    第{{index+1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time|dataFormat}}
                </div>
                <div class="content">
                   {{item.content}}
                </div>
            </li>
        </ul>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>

    </div>
</template>

<script>
    import {Toast} from 'mint-ui'
    export default {
        data(){
            return{
                pageIndex:1,
                comments:[],
                msg:''
            }
        },
        created(){
          this.getComments();
        },
        methods:{
            getComments(){
                this.$http.get("api/getcomments/"+this.id +"?pageindex="+this.pageIndex).then(result=>{
                    console.log(result.body.message);
                    // this.comments=result.body.message;
                    this.comments=this.comments.concat(result.body.message)
                })
            },
            getMore(){
                this.pageIndex++;
                this.getComments();
            },
            postComment(){
                console.log(this.id);
                console.log(this.$route.params.id);
                if(this.msg.trim().length==''){
                    return Toast("发表内容不能为空");
                }
                this.$http.post("api/postcomment/"+this.id,{content:this.msg.trim()}).then(result=>{
                   var cmt={
                       user_name:"匿名用户",
                       add_time:Date.now(),
                       content:this.msg.trim()
                   };
                   this.comments.unshift(cmt);
                   this.msg='';
                })
            }

        },
        props:['id']
    }
</script>

<style scoped>
.comment-container h3{
    font-size: 18px;
}
.comment-container  textarea{
    height: 85px;
    font-size: 12px;
    margin: 0px;

}
.comment-container .cmt-list{
    list-style: none;
    width: 100%;
    margin: 5px 0;
    padding: 0;
}
.comment-container .cmt-list .cmt-title{
    font-size: 12px;
    background-color: #ccc;
}
.comment-container .cmt-list  .content{
    text-indent: 2em;
    font-size: 14px;
    line-height: 35px;
}
</style>